/**
 * @Author 天下无敌
 * @Date 资源管理系统
 * @AIDE AIDE+ 
 */
package com.ecs.core.gsystem;

import com.artemis.BaseSystem;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.ecs.core.debug.Debug;

public class AssetsSystem extends BaseSystem {
    private final AssetManager assetManager;
    private boolean loading = false;

    public AssetsSystem(AssetManager assetManager) {
		super();
        this.assetManager = assetManager;
    }

    @Override
    protected void processSystem() {
        // 如果正在加载且资源管理器更新完成，则标记加载完成
       if (loading && assetManager.update()) {
            loading = false;
            Debug.log("AssetsSystem", "Assets loading completed");
        }
    }

	public AssetManager getAsset(){
		return assetManager;
	}
    /**
     * 异步加载资源
     * @param path 资源路径
     * @param type 资源类型
     */
    public <T> void load(String path, Class<T> type) {
        if (!assetManager.isLoaded(path, type)) {
            assetManager.load(path, type);
            loading = true;
        }
    }

    /**
     * 异步加载纹理
     */
    public void loadTexture(String path) {
        load(path, Texture.class);
    }

    /**
     * 异步加载位图字体
     */
    public void loadFont(String path) {
        load(path, BitmapFont.class);
    }

    /**
     * 异步加载纹理图集
     */
    public void loadTextureAtlas(String path) {
        load(path, TextureAtlas.class);
    }

    /**
     * 异步加载音效
     */
    public void loadSound(String path) {
        load(path, Sound.class);
    }

    /**
     * 异步加载音乐
     */
    public void loadMusic(String path) {
        load(path, Music.class);
    }

    /**
     * 获取资源
     * @param path 资源路径
     * @param type 资源类型
     * @return 加载完成的资源
     */
    public <T> T get(String path, Class<T> type) {
        if (assetManager.isLoaded(path, type)) {
            return assetManager.get(path, type);
        }
        Debug.log("AssetsSystem", "Asset not loaded: " + path);
        return null;
    }

    /**
     * 获取纹理
     */
    public Texture getTexture(String path) {
        return get(path, Texture.class);
    }

    /**
     * 获取位图字体
     */
    public BitmapFont getFont(String path) {
        return get(path, BitmapFont.class);
    }

    /**
     * 获取纹理图集
     */
    public TextureAtlas getTextureAtlas(String path) {
        return get(path, TextureAtlas.class);
    }

    /**
     * 获取音效
     */
    public Sound getSound(String path) {
        return get(path, Sound.class);
    }

    /**
     * 获取音乐
     */
    public Music getMusic(String path) {
        return get(path, Music.class);
    }

    /**
     * 卸载资源
     * @param path 资源路径
     */
    public void unload(String path) {
        if (assetManager.isLoaded(path)) {
            assetManager.unload(path);
        }
    }

    /**
     * 卸载所有资源
     */
    public void unloadAll() {
        assetManager.clear();
    }

    /**
     * 获取当前加载进度
     * @return 0-1的进度值
     */
    public float getProgress() {
        return assetManager.getProgress();
    }

    /**
     * 是否正在加载资源
     */
    public boolean isLoading() {
        return loading;
    }

    /**
     * 阻塞式完成所有加载任务
     */
    public void finishLoading() {
        assetManager.finishLoading();
        loading = false;
    }
}
